package com.nd.service;

import cn.hutool.core.date.DateTime;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nd.dao.KeywordMapper;
import com.nd.pojo.bean.Keyword;
import com.nd.pojo.bean.KeywordExample;
import com.nd.pojo.bo.KeywordCreateBO;
import com.nd.pojo.vo.KeywordCreateVO;
import com.nd.pojo.vo.KeywordListVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description:
 * @author: Aaron
 * @create: 2021-06-06 13:34
 **/
@Service
public class KeywordServiceImpl implements KeywordService {
    @Autowired
    KeywordMapper keywordMapper;
    @Override
    public KeywordListVO keywordList(Integer page, Integer limit, String keyword, String url, String sort, String order) {
        PageHelper.startPage(page,limit);
        KeywordExample keywordExample = new KeywordExample();
        keywordExample.setOrderByClause(sort + " " + order);
        KeywordExample.Criteria criteria = keywordExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        if (keyword != null && !"".equals(keyword)) criteria.andKeywordLike("%" + keyword + "%");
        if (url != null && !"".equals(url)) criteria.andUrlLike("%" + url + "%");
        List<Keyword> keywords = keywordMapper.selectByExample(keywordExample);
        PageInfo<Keyword> keywordPageInfo = new PageInfo<>(keywords);
        long total = keywordPageInfo.getTotal();
        KeywordListVO keywordListVO = new KeywordListVO();
        keywordListVO.setTotal(total);
        keywordListVO.setItems(keywords);
        return keywordListVO;
    }

    @Override
    public KeywordCreateVO keywordCreate(KeywordCreateBO keywordCreateBO) {
        Keyword keyword = new Keyword();
        keyword.setKeyword(keywordCreateBO.getKeyword());
        keyword.setUrl(keywordCreateBO.getUrl());
        keyword.setIsHot(keywordCreateBO.getIsHot());
        keyword.setIsDefault(keywordCreateBO.getIsDefault());
        Integer max = keywordMapper.keywordCreateGetMaxSortOrder();
        keyword.setSortOrder(++max);
        DateTime time = DateTime.now();
        keyword.setAddTime(time);
        keyword.setUpdateTime(time);
        keywordMapper.keywordCreate(keyword);
        Integer id = keywordMapper.keywordGetId();

        KeywordCreateVO keywordCreateVO = KeywordCreateVO.builder()
                .id(id)
                .keyword(keywordCreateBO.getKeyword())
                .url(keywordCreateBO.getUrl())
                .isHot(keywordCreateBO.getIsHot())
                .isDefault(keywordCreateBO.getIsDefault())
                .addTime(time)
                .updateTime(time)
                .build();

        return keywordCreateVO;
    }

    @Override
    public Keyword keywordUpdate(Keyword keyword) {
        keyword.setUpdateTime(DateTime.now());
        //Keyword keyword1 = keywordMapper.selectByPrimaryKey(keyword.getId());
        //KeywordExample keywordExample = new KeywordExample();
        keywordMapper.updateByPrimaryKey(keyword);
//        KeywordExample.Criteria criteria = keywordExample.createCriteria();
//        criteria.andIdEqualTo(keyword.getId()).andKeywordEqualTo(keyword.getKeyword()).andUrlEqualTo(keyword.getKeyword()).andIsHotEqualTo(keyword.getIsHot()).andIsDefaultEqualTo(keyword.getIsDefault()).andSortOrderEqualTo(keyword.getSortOrder()).andAddTimeEqualTo(keyword.getAddTime()).andUpdateTimeEqualTo(time).andDeletedEqualTo(keyword.getDeleted());
//        keywordMapper.updateByExample(keyword1,keywordExample);
        //keywordMapper.updateTime(time,keyword.getId());

        return keyword;
    }

    @Override
    public void keywordDelete(Keyword keyword) {
        keywordMapper.keywordDelete(keyword.getId());
    }
}
